/*
 * Copyright 2013 Shaun McCloud
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *  http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
/* 
 * Sample code for menu found at http://www.codeproject.com/Articles/3562/Text-based-menu-class-for-Console-Applications
*/
using System;
using System.Collections;

delegate void MenuCallback();

class Menu
{
	private class MenuItem
	{
		public MenuCallback mc;
		public string text;
		
		public MenuItem(string Text, MenuCallback Mc)
		{
			mc = Mc;
			text = Text;
		}
	}
		
	private ArrayList m_Items = new ArrayList();
	
	public void Add(string text,MenuCallback mc)
	{
		m_Items.Add(new MenuItem(text,mc));
	}
	
	public void Show()
	{
		for(int i = 0;i<m_Items.Count;++i)
		{
			MenuItem mi = m_Items[i] as MenuItem;
			Console.WriteLine(" [{0}] {1}", i+1, mi.text);
		}
		
		int choosen = Int32.Parse(Console.ReadLine());
		
		if ( choosen > m_Items.Count || choosen < 1 )
		{
			Console.WriteLine("Invalid option.");
		}
		else
		{
			MenuItem mi = m_Items[choosen-1] as MenuItem;
			MenuCallback mc = mi.mc;
			mc();
		}
	}
	
}